package com.pwc.conversionTool.controller;


import java.io.File;
import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.pwc.conversionTool.config.impl.XMLConfigImpl;
import com.pwc.conversionTool.csvProcess.CsvProcess;
@Controller
public class CsvController {
	//log 4j
	private static final Logger logger = LoggerFactory.getLogger(CsvController.class);
	
	
	//field
	private CsvProcess progree;
	
	
	@Autowired
	public ServletContext context;
	@RequestMapping(value = "/selectCsv", method = RequestMethod.GET)
	public String selectCsv(Locale locale, Model model) throws IOException {
		
		System.out.println("**********server path:"+context.getRealPath(""));
		progree=new CsvProcess(context.getRealPath(""));
		
		progree.getTablecontent();
		//model.addAttribute("head", progree.getTablehead());//value is object
		//model.addAttribute("content", progree.getTablecontent());//value is object
		
		return "selectCsv";
	}
	
	
	@RequestMapping(value = "/deleteCsv", method = RequestMethod.GET)
	public String deleteCsv(Locale locale, Model model) throws IOException {
		
		System.out.println("**********server path:"+context.getRealPath(""));
		progree=new CsvProcess(context.getRealPath(""));
		
		progree.getTablecontent();
		//model.addAttribute("head", progree.getTablehead());//value is object
		//model.addAttribute("content", progree.getTablecontent());//value is object
		
		return "selectCsv";
	}
	
	
	
	
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/csv/upload", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		return "csv/upload";
	}
	
	/**
	    * Upload single file using Spring Controller
	    */
//	   @RequestMapping(value = "/csv/uploadFile", method = RequestMethod.POST)
//	   public String uploadFileHandler(@RequestParam("fileName") String fileName,
//	           @RequestParam("file") MultipartFile file, Model model) {
//
//		   String message="";
//		   
//	       if (!file.isEmpty()) {
//	           try {
//	        	   File folderFile=new File(XMLConfigImpl.initial(context).getUploadPath());
//	        	   
//	               byte[] bytes = file.getBytes();
//	               
//	               String FileName=fileName.equals("")?file.getOriginalFilename():fileName;
//	               
//	               String path=folderFile.getPath()+File.separator+FileName;
//	               
//	               File saveFile=new File(path);
//	               
//	               if(!saveFile.exists())
//	               {
//	            	   FileUtils.writeByteArrayToFile(saveFile, bytes);
//	               }else {
//	            	   saveFile=new File(context.getContextPath()+File.separator+System.currentTimeMillis()+FileName);
//	            	   FileUtils.writeByteArrayToFile(saveFile, bytes);
//				}
//	               
//	               message= "upload success";
//	           } catch (Exception e) {
//	        	   message= " failed to upload "  + e.getMessage();
//	           }
//	       } else {
//	    	   message= " failed to upload , because the file was empty.";
//	       }
//	       
//	       model.addAttribute("msg", message);
//	       return "csv/upload";
//	       
//	   }


	
	
}
